<?php
class C_document extends CI_Controller {
	function __construct(){
		parent::__construct();
		checkLogin();
		$this->load->model('m_document');
	}
	function index(){
		$task = secure_post('task');
		switch($task){
			case 'LIST' :
				$this->_list();
				break;
			case 'INSERT' :
				$this->_insert();
				break;
			case 'UPDATE' :
				$this->_update();
				break;
			case 'DELETE' :
				$this->_delete();
				break;
			case 'PROCESS' :
				$this->_process();
				break;
			default :
				$div_id = @$_SESSION[SESSION_DIV];
				$data["flow"] = numeric_post('flow');
				$code = $this->m_document->flowcodebyid($data["flow"]);
                $data["flow_code"] = $code->flow_code;
                $data["flow_subcode"] = $code->flow_subcode;
				$data["pos"] = $this->m_document->posflowbydiv($data["flow"], $div_id);
				$data["last"] = $this->m_document->poslastflow($data["flow"]);
				$this->load->view('v_document',$data);
		}
	}
	
	function _list(){
		$data['page'] = numeric_post("page");
		$data['rows'] = numeric_post("rows");
		$data['document_noall'] = nosql_post("document_noall");
		$data['project_name'] = nosql_post("project_name");
		$data['project_code'] = nosql_post("project_code");
		$data['m_flow_id'] = nosql_post("flow_id");
		echo $this->m_document->_list($data);
	}
	
	function _insert(){
        $m_flow_id = $this->input->post('m_flow_id');
        $t_project_id = $this->input->post('t_project_id');
        
		$data = array(
			'document_noflow' => $this->input->post('document_noflow'), 
			'document_noproyek' => $this->input->post('document_noproyek'), 
			'document_no' => $this->input->post('document_no'),
			'document_holdpoint' => $this->input->post('document_holdpoint'),
			't_project_id' => $this->input->post('t_project_id'), 
			'm_flow_id' => $m_flow_id, 
			  
			'document_insert_user'	=>$_SESSION[SESSION_USERNAMA],
			'document_insert_date'=> date("Y-m-d h:i:s"));
		
		$document_id = $this->m_document->_insert($data);
		echo 1;
	}
	
	function _update(){
		$document_id = $this->input->post('document_id');
		$data = array(
			'document_noflow' => $this->input->post('document_noflow'), 
			'document_noproyek' => $this->input->post('document_noproyek'), 
			'document_no' => $this->input->post('document_no'), 
			't_project_id' => $this->input->post('t_project_id'), 
			'm_flow_id' => $this->input->post('m_flow_id'), 
			
			'document_update_user'	=>$_SESSION[SESSION_USERNAMA],
			'document_update_date'=> date("Y-m-d h:i:s"));
		$this->m_document->_update($data,$document_id);
		echo 1;
 	}
	
	function _delete(){
		$document_id= $this->input->post('document_id');
		$data = array(
			'document_id' => $document_id
		);
		$this->m_document->_delete($data);
		echo 1;
	}
	
	function _process(){
		$nextuser = numeric_post('nextuser');
		$flag = numeric_post('process_flag');
		$checklist =$this->input->post('checklist');
		
		$t_document_id = $this->input->post('t_document_id');
		$max_tracking_seq = numeric_post('max_tracking_seq');
		
        $tracking_keterangan = $this->input->post("tracking_keterangan");
		if ($nextuser){
			$is_update = $this->m_document->is_update($t_document_id, $max_tracking_seq);
			if ($is_update){
				$where = array(
					't_document_id' => $t_document_id,
					'tracking_seq' =>  $max_tracking_seq  
				);
				
				$data = array(  
					'tracking_date' => date("Y-m-d h:i:s"), 
					'tracking_flag' => $flag, 
					'tracking_keterangan' => $tracking_keterangan, 
					  
					'tracking_update_user'	=>$_SESSION[SESSION_USERNAMA],
					'tracking_update_date'=> date("Y-m-d h:i:s"));
				$this->m_document->_process($data, $checklist, $where);
			}else{
				$data = array(
					'tracking_seq' =>  $max_tracking_seq  + 1, 
					't_document_id' => $t_document_id, 
					'tracking_date' => date("Y-m-d h:i:s"), 
					'tracking_flag' => $flag, 
					'tracking_keterangan' => $tracking_keterangan, 
					  
					'tracking_insert_user'	=>$_SESSION[SESSION_USERNAMA],
					'tracking_insert_date'=> date("Y-m-d h:i:s"));
				$this->m_document->_process($data, $checklist, false);
			}
		}else{
			$data = array(
				'tracking_seq' =>  $max_tracking_seq  + 1, 
				't_document_id' => $t_document_id, 
				'tracking_date' => date("Y-m-d h:i:s"), 
				'tracking_flag' => 5, 
                'tracking_keterangan' => $tracking_keterangan, 
				  
				'tracking_insert_user'	=>$_SESSION[SESSION_USERNAMA],
				'tracking_insert_date'=> date("Y-m-d h:i:s"));
			$this->m_document->_process($data, $checklist, false);
		}
		
		echo 1;
	}
}